/**
 * Created by yuris on 09.02.15.
 */
package view {
import InterfaceObjects.OrdetProductWindow.OrderProductWindow;
import InterfaceObjects.ShopObjectContextMenu;

import com.greensock.TweenMax;
import com.greensock.easing.Back;
import com.greensock.easing.Bounce;
import com.greensock.easing.Cubic;
import com.greensock.easing.Elastic;
import com.greensock.easing.Quad;
import com.greensock.easing.Quart;
import com.greensock.easing.Quint;

import layers.InterfaceLayer;

import messages.ModelToViewMessage;

import model.Constants;

import model.LocalizationProxy;
import model.objects.ProductData;
import model.objects.ProductWarehouseData;

import mvcexpress.mvc.Mediator;

import starling.display.Image;
import starling.events.EnterFrameEvent;

public class InterfaceLayerMediator extends Mediator {
    [Inject]
    public var interfaceLayer:InterfaceLayer;

    private var _orderProductWindow:OrderProductWindow;

    private var _productSingleIcon:Image;



    override protected function onRegister():void {
        mediatorMap.mediate(interfaceLayer.bottomPanel);

        addHandler(ModelToViewMessage.LOCALIZATION_INIT_COMPLETE, HANDLER_localizationInit);

        //addHandler(ModelToViewMessage.SHOW_WINDOW_ORDER_PRODUCTS, HANDLER_toggleShowOrderProductsWindow);

        addHandler(ModelToViewMessage.NEW_PRODUCT_ORDER_ANIMATION, HANDLER_newProductOrderAnimation);

    }



    private var _productOrderAnimationFlag:Boolean = false;
    private function HANDLER_newProductOrderAnimation(params:Object):void {
        if(this._productSingleIcon != null){
            this._productSingleIcon.removeFromParent()
            this._productSingleIcon.dispose();
            this._productSingleIcon = null;
        }
        this._productSingleIcon = GraphicFactory.getImageByName(Constants.PATH_PRODUCT_ICONS_SPRITES+"/icon_"+params.sprite_id);

        this._productOrderAnimationFlag = true;

        this._productSingleIcon.x = params.start.x;
        this._productSingleIcon.y = params.start.y;
        interfaceLayer.addChild(this._productSingleIcon);
        this._productSingleIcon.alpha = 0;
        TweenMax.to(this._productSingleIcon,0.8,{x:params.end.x,ease: Quad.easeOut})
        TweenMax.to(this._productSingleIcon,0.3,{y:this._productSingleIcon.y - 100,alpha:1,ease: Quad.easeOut})
        TweenMax.to(this._productSingleIcon,0.5,{delay:0.3,y:params.end.y, ease:Quad.easeIn, onComplete:this._productSingleIcon.removeFromParent, onCompleteParams:[true]})
        TweenMax.to(this._productSingleIcon,0.5,{delay:0.8,alpha:0.5, onComplete:this._endProductOrderAnimation});

    }

    private function _endProductOrderAnimation():void {
        this._productOrderAnimationFlag = false;
    }



    private function HANDLER_localizationInit(loc:LocalizationProxy):void {
        var _branch:String = "order_product_window";

        this._orderProductWindow = new OrderProductWindow(loc.getLocale(_branch,"title"), [ loc.getLocale(_branch,"tab1"),
                                                                                            loc.getLocale(_branch,"tab2"),
                                                                                            loc.getLocale(_branch,"tab3"),
                                                                                            loc.getLocale(_branch,"tab4")]);


        this._orderProductWindow.width = 590;
        this._orderProductWindow.height = 540 - 500;
        this._orderProductWindow.x = interfaceLayer.stage.stageWidth/2;
        this._orderProductWindow.y = 350 //+1000;
        this._orderProductWindow.visible = false;
        interfaceLayer.addChild(this._orderProductWindow);

        mediatorMap.mediate(this._orderProductWindow);
    }

    override protected function onRemove():void {

    }
}
}
